
*Section 1 Bring in Main Data
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************

*Bring in V-Party Data

cd "/Users/john/Dropbox/PartyDuration/Data/Replication"
use V-Dem-CPD-Party-V1, clear

rename COWcode cowcode
rename country_name country

*Vietnam cowcode merging
replace cowcode=818 if cowcode==816

*Trim party name
gen x = substr(v2paenname,1,125)
rename x party_name

**Merge in Reuter ruling party data in order to add regime_ids
sort cowcode year
merge cowcode year using merge_reuter

**Fix  an error:  Odd regimeid overlap with Laos and Bahrain
replace regimeid=917 if cowcode==812 & year<1976 & year>1971



*Section 2 Establish set of ruling parties
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************

gen ruling_party=1 if v2pagovsup==0 & fail!=.
gen non_ruling=1 if pf_party_id!=. & ruling_party!=1



**Fixing some of the discrepancies with Miller data
*Haiti
replace ruling_party=1 if pf_party_id==5273
replace ruling_party=1 if pf_party_id==5268 & year==2000
replace ruling_party=1 if pf_party_id==5271
*El Salvador
replace ruling_party=1 if pf_party_id==1709
replace ruling_party=. if pf_party_id==6211
replace non_ruling=1 if pf_party_id==6211

*Guatemala
replace ruling_party=. if pf_party_id==4437
replace non_ruling=1 if pf_party_id==4437
*Spain
replace ruling_party=1 if pf_party_id==5778
*East Germany
replace ruling_party=1 if pf_party_id==4658

*Poland
replace ruling_party=. if pf_party_id==4473
replace non_ruling=1 if pf_party_id==4473
replace ruling_party=. if pf_party_id==6154
replace non_ruling=1 if pf_party_id==6154

*Sao Tome
replace ruling_party=1 if pf_party_id==5331
*Guinea Bissau
*replace ruling_party=1 if pf_party_id==4236

**Liberia
replace ruling_party=. if pf_party_id==4140 & year==1985
replace non_ruling=1 if pf_party_id==4140 & year==1985

**Togo
replace ruling_party=. if pf_party_id==6613 & year==1994
replace non_ruling=1 if pf_party_id==6613 & year==1994

**Congo
replace ruling_party=. if pf_party_id==5364 & year==1993
replace non_ruling=1 if pf_party_id==5364 & year==1993


**Ethiopia
replace ruling_party=. if pf_party_id==6589 & year==2000
replace non_ruling=1 if pf_party_id==6589 & year==2000
replace ruling_party=. if pf_party_id==5843 & year==2000
replace non_ruling=1 if pf_party_id==5843 & year==2000
**Zimbabwe
replace ruling_party=. if pf_party_id==6034 & year==1979
replace non_ruling=1 if pf_party_id==6034 & year==1979





*Niger
replace ruling_party=1 if pf_party_id==5347
replace ruling_party=1 if pf_party_id==2350
*Democratic Republic of Congo
replace ruling_party=1 if pf_party_id==5090

**Lesotho
replace ruling_party=1 if pf_party_id==4171

replace ruling_party=1 if pf_party_id==3771

replace ruling_party=. if pf_party_id==4171 & year==1998
replace non_ruling=1 if pf_party_id==4171 & year==1998

replace ruling_party=. if pf_party_id==4167 & year==1998
replace non_ruling=1 if pf_party_id==4167 & year==1998

*Nadagascar
replace ruling_party=. if pf_party_id==5692 
replace non_ruling=1 if pf_party_id==5692 

**Comoros
replace ruling_party=1 if pf_party_id==5296
replace ruling_party=1 if pf_party_id==5298
replace ruling_party=1 if pf_party_id==6582

*Afghanistan
replace ruling_party=1 if pf_party_id==5184

*Sierra Leone
replace ruling_party=. if year==2007 & pf_party_id==4045
replace non_ruling=1 if year==2007 & pf_party_id==4045

replace ruling_party=. if year==2012 & pf_party_id==4045
replace non_ruling=1 if year==2012 & pf_party_id==4045

**Cambodia
replace ruling_party=. if year==1993 & pf_party_id==3764
replace non_ruling=1 if year==1993 & pf_party_id==3764

**Bhutan (unclear what is going on here)
replace fail=. if (year==2014 | year==2015) & cowcode ==760

**Bulgaria was missing because it is coded as junior partner
replace ruling_party=1 if pf_party_id==6189 

**PS in Senegal was missing because it is coded as not represented in govt
replace ruling_party=1 if pf_party_id==2380 & year<2001

****Com party in Vietnam was missing because it is coded as not represented in govt
replace ruling_party=1 if pf_party_id==6900

***Angola pre 1992 was oddly missing
replace ruling_party=1 if pf_party_id==2298

***Adding some missing ruling parties from Miller
*Sao Tome
replace ruling_party=1 if pf_party_id==5331 & year<1991
*Seychelles
replace ruling_party=1 if pf_party_id==3847 & year>1976 
*Maldives
replace ruling_party=. if pf_party_id==5329 | pf_party_id==5684
replace ruling_party=1 if pf_party_id==5682



**Keep only ruling party observations
drop if non_ruling==1 & ruling_party!=1
drop regimeid pres_regime_sample1 leg_regime_sample polity2_full polrights fail fail_type fail_subsregime ideology_rlc anyideology left domparty1sudanzambia fail_type2 _merge
sort cowcode year


merge cowcode year using merge_reuter
drop _merge
sort cowcode year
merge cowcode year using gwf_stuff
drop _merge

**Odd regimeid overlap with Laos and Bahrain
replace regimeid=917 if cowcode==812 & year<1976 & year>1971



**Fixig more errors
*Sao Tome
replace regimeid=919 if cowcode==403 & year>1974 & year<1991
replace fail=0 if cowcode==403 & year>1974 & year<1990
replace fail=1 if cowcode==403 & year==1990
*Seychelles
replace regimeid=920 if cowcode==591 & year>1976
replace fail=0 if cowcode==591 & year>1976
*Comoros
replace ruling_party=1 if pf_party_id==5298
replace fail=1 if cowcode==581 & year==1995 

*Maldives
replace regimeid=428 if year>2013 & year<2016 & cowcode==781
replace fail=0 if year>2013 & year<2016 & cowcode==781



*Section 3
***Creating Country-Year Format and fixing errors
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************




sort regimeid year
gen p_regime=ruling_party
by regimeid: carryforward p_regime, replace

sort cowcode year
by cowcode: carryforward country, replace

duplicates drop


**Odd things missing
replace fail=0 if cowcode==40 & (year==2011 | year==2012 | year==2013)
replace regimeid=79 if cowcode==40 & (year==2011 | year==2012 | year==2013)
replace regimeid=415 if cowcode==694 & ( year==2014 | year==2015)
replace regimeid=263 if cowcode==696 & ( year==2014 | year==2015)
replace regimeid=268 if cowcode==818 & ( year==2014 | year==2015)


**Creating a sample because fail and regimeid include lots of democracies.  Leg_regime_sample excludes a lot of single party regimes
gen sample=1 if leg_regime_sample==1 & fail~=. & regimeid~=. & year>1972
replace sample=1 if fail~=. & regimeid~=. & year>1972 & polity2_full<6 & polrights>2
rename sample auth_sample

**Odd things missing
replace auth_sample=1 if cowcode==40 & ( year==2014 | year==2015)
replace auth_sample=1 if cowcode==531 & ( year==2014 | year==2015)
replace auth_sample=1 if cowcode==694 & ( year==2014 | year==2015)
replace auth_sample=1 if cowcode==696 & ( year==2014 | year==2015)
replace auth_sample=1 if cowcode==710 & ( year==2014 | year==2015)
replace auth_sample=1 if cowcode==731 & ( year==2014 | year==2015)
replace auth_sample=1 if cowcode==812 & ( year==2014 | year==2015)
replace auth_sample=1 if cowcode==818 & ( year==2014 | year==2015)

***Fixing oddly missing regimeids
replace regimeid=79 if (year==2014 | year==2015) & cowcode==40
replace regimeid=93 if (year==2014 | year==2015) & cowcode==531
replace regimeid=181 if (year==2014 | year==2015) & cowcode==565
replace regimeid=68 if (year==2014 | year==2015) & cowcode==710
replace regimeid=146 if (year==2014 | year==2015) & cowcode==731
replace regimeid=433 if (year==2014) & cowcode==790
replace regimeid=443 if (year==2014 | year==2015) & cowcode==800
replace regimeid=154 if (year==2014 | year==2015) & cowcode==812




**Fixing some duplicates
drop if cowcode==435 & year==2013 & ruling_party~=1

*Some missing auth_sample
replace auth_sample=1  if fail==1 & year>1972 & auth_sample!=1 & auth_sample[_n-1]==1 & regimeid[_n-1]==regimeid
replace auth_sample=1 if cowcode==265 & (year==1989 | year==1990)
replace auth_sample=1 if cowcode==365 & (year<1992 & year>1971)

replace auth_sample=1 if cowcode==516 & year<2011 & year>2005
sort cowcode year 


*Section 4
***Creating Ruling Party Variables
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************




**Setting up country-year party id
gen rparty_id=pf_party_id
**Addressing issue where the ruling party is the winner of previous election
replace rparty_id=rparty_id[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_id, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_id=rparty_id[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_id[_n-1]~=rparty_id





**Setting up country-year party vote share
gen rparty_vote=v2pavote
**Addressing issue where the ruling party is the winner of previous election
replace rparty_vote=rparty_vote[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_vote, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_vote=rparty_vote[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_vote[_n-1]~=rparty_vote




**Setting up country-year party seat share
gen rparty_seat=v2paseatshare
**Addressing issue where the ruling party is the winner of previous election
replace rparty_seat=rparty_seat[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_seat, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_seat=rparty_seat[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_seat[_n-1]~=rparty_seat


***Total strength
gen share=rparty_seat
replace share=rparty_vote if share==. 




**Setting up country-year local org strength
gen rparty_lorg=v2paactcom
**Addressing issue where the ruling party is the winner of previous election
replace rparty_lorg=rparty_lorg[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_lorg, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_lorg=rparty_lorg[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_lorg[_n-1]~=rparty_lorg


**Setting up country-year local org strength.  OSP versgion
gen rparty_lorg_o=v2paactcom_osp
**Addressing issue where the ruling party is the winner of previous election
replace rparty_lorg_o=rparty_lorg_o[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_lorg_o, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_lorg_o=rparty_lorg_o[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_lorg_o[_n-1]~=rparty_lorg_o




**Setting up country-year personalization strength
gen rparty_pers=v2paind
**Addressing issue where the ruling party is the winner of previous election
replace rparty_pers=rparty_pers[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_pers, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_pers=rparty_pers[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_pers[_n-1]~=rparty_pers





**Setting up country-year personalization strength
gen rparty_pers_o=v2paind_osp
**Addressing issue where the ruling party is the winner of previous election
replace rparty_pers_o=rparty_pers_o[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_pers_o, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_pers_o=rparty_pers_o[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_pers_o[_n-1]~=rparty_pers_o







**Setting up country-year cohesion
gen rparty_cohes=v2padisa
**Addressing issue where the ruling party is the winner of previous election
replace rparty_cohes=rparty_cohes[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_cohes, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_cohes=rparty_cohes[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_cohes[_n-1]~=rparty_cohes


**Setting up country-year cohesion ordinal
gen rparty_cohes_ord=v2padisa_ord
**Addressing issue where the ruling party is the winner of previous election
replace rparty_cohes_ord=rparty_cohes_ord[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_cohes_ord, replace
sort cowcode year





***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_cohes_ord=rparty_cohes_ord[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_cohes_ord[_n-1]~=rparty_cohes_ord


gen defections=1 if rparty_cohes_ord==0 | rparty_cohes_ord==1
replace defections=0 if rparty_cohes_ord>1 & rparty_cohes_ord<5

 **Reverse code schisms
 gen rparty_schism=rparty_cohes*-1


**Setting up country-year clientelism
gen rparty_client=v2paclient
**Addressing issue where the ruling party is the winner of previous election
replace rparty_client=rparty_client[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_client, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_client=rparty_client[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_client[_n-1]~=rparty_client







**Setting up country-year local offices
gen rparty_off=v2palocoff
**Addressing issue where the ruling party is the winner of previous election
replace rparty_off=rparty_off[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_off, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_off=rparty_off[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_off[_n-1]~=rparty_off



**Setting up country-year local offices osp version
gen rparty_off_o=v2palocoff_osp
**Addressing issue where the ruling party is the winner of previous election
replace rparty_off_o=rparty_off_o[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_off_o, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_off_o=rparty_off_o[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_off_o[_n-1]~=rparty_off_o










**Setting up country-year social org. ties
gen rparty_soc=v2pasoctie
**Addressing issue where the ruling party is the winner of previous election
replace rparty_soc=rparty_soc[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_soc, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_soc=rparty_soc[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_soc[_n-1]~=rparty_soc







**Setting up country-year social org. ties osp
gen rparty_soc_o=v2pasoctie_osp
**Addressing issue where the ruling party is the winner of previous election
replace rparty_soc_o=rparty_soc_o[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_soc_o, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_soc_o=rparty_soc_o[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_soc_o[_n-1]~=rparty_soc_o





**Setting up country-year candidate nominations 
gen rparty_nom=v2panom
**Addressing issue where the ruling party is the winner of previous election
replace rparty_nom=rparty_nom[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_nom, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_nom=rparty_nom[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_nom[_n-1]~=rparty_nom






**Setting up country-year candidate nominations 
gen rparty_nom_ord=v2panom_ord
**Addressing issue where the ruling party is the winner of previous election
replace rparty_nom_ord=rparty_nom_ord[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_nom_ord, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_nom_ord=rparty_nom_ord[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_nom_ord[_n-1]~=rparty_nom_ord


**Binary 
gen leader_nominates=1 if rparty_nom_ord==0
replace leader_nominates=0 if rparty_nom_ord~=0 & rparty_nom_ord~=.

**Reverse
gen rparty_nom_rev=-1*rparty_nom



**Setting up country-year candidate nominations osp
gen rparty_nom_o=v2panom_osp
**Addressing issue where the ruling party is the winner of previous election
replace rparty_nom_o=rparty_nom_o[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_nom_o, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_nom_o=rparty_nom_o[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_nom_o[_n-1]~=rparty_nom_o









**Setting up country-year right/left
gen rparty_rl=v2pariglef
**Addressing issue where the ruling party is the winner of previous election
replace rparty_rl=rparty_rl[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_rl, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_rl=rparty_rl[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_rl[_n-1]~=rparty_rl




**Setting up country-year ethnic party
gen rparty_eth=v2paculsup
**Addressing issue where the ruling party is the winner of previous election
replace rparty_eth=rparty_eth[_n-1] if auth_sample==1 & regimeid[_n-1]~=regimeid & fail[_n-1]==1 & year[_n-1]==year-1 & pf_party_id[_n-1]~=. & (fail_type[_n-1]==2 | fail_type[_n-1]==3 | fail_type==1)
sort regimeid year
by regimeid: carryforward rparty_eth, replace
sort cowcode year



***Dealing with cases where the new winning party in an election holds the value for the last year of previous regime
replace rparty_eth=rparty_eth[_n-1] if regimeid[_n-1]==regimeid & fail==1 & rparty_eth[_n-1]~=rparty_eth




replace rparty_eth=rparty_eth*-1


***Coding Ideology

*Deviation from mean yearly ideology
sort year
by year: egen year_ideology_mean=mean(rparty_rl)
sort cowcode year
gen ideology_year=rparty_rl-year_ideology_mean


*Deviation from historical ideology
gen ideology_hist=rparty_rl
replace ideology_hist=rparty_rl*-1 if ideology_hist<0 & ideology_hist~=.


*Section 6
***Miscellaneous
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************


**Odd cases removed
*Military caretaker govt and it was taking on values of transitional election held in December.
replace auth_sample=0 if year==2008 & cowcode==771


***Dealing with a couple transitional regimes where the election comes after the coup and should not be counted as ruling party in that year.  See DataonSomeOddCases.xls
foreach var of varlist rparty_id rparty_cohes rparty_lorg rparty_off rparty_nom rparty_soc rparty_pers share{
replace `var'=. if (year==1992 & cowcode==372) | (year==2000 & cowcode==437)
}





**Creating a counter
sort regimeid year

by regimeid: egen t=seq()
**Fixing some t values for the g and b analyses
replace t=1 if year==1982 & cowcode==92
replace t=2 if year==1983 & cowcode==92

replace t=1 if year==2006 & cowcode==41
replace t=1 if year==2004 & cowcode==404
replace t=2 if year==2005 & cowcode==404
replace t=. if year==2013 & cowcode==404
replace t=. if year==2015 & cowcode==439
replace t=1 if year==1992 & cowcode==484
replace t=. if cowcode==645 & year>2004 & year<2010
gen t2=t^2
gen t3=t^3

sort cowcode year


*br cowcode year  fail t regimeid pf_party_id ruling_party rparty_id share rparty_vote v2pavote auth_sample  pf_party_id year country gwf_country polrights polity2_full leg_regime_sample v2pavote v2pagovsup v2paseatshare v2paenname if e(sample)~=1 & auth_sample==1 & fail~=. 

**Missing data on vote/seat shares
replace share=share_reuter if share==. & share_reuter~=.

**Missing polity
replace polity2_full=polity2 if polity2_full==.

*Lag Polity because it is December 31
sort regimeid year
by regimeid: gen lag_polity=polity2_full[_n-1]
sort cowcode year



*Section 7
***Merging Some Necessary Data
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************


save duration, replace

**Vdem

sort cowcode year
merge cowcode year using vdem_merge
drop _merge


*To see which of missing are non-party regimes
sort cowcode year
merge cowcode year using merge_miller
drop _merge


***Merging Geddes party variables
sort cowcode year
merge cowcode year using gwf_merge

**Merge Party Age
drop Party_Age
drop _merge
sort cowcode year
merge cowcode year using partyage_final
gen log_age=ln(Party_Age)

*To look at missings...
*br cowcode year  fail t regimeid pf_party_id ruling_party gwf_regimetype Party_Regime rparty_id share  year country gwf_country  if e(sample)~=1 & auth_sample==1 & rparty_lorg==.



*Section 8
***Coding some other variables
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************

**code for a no party regime
gen no_party=1 if Party_Regime==0 & rparty_id==.
replace no_party=1 if cowcode==130 & year==1979
replace no_party=1 if cowcode==145 & year==1979
replace no_party=1 if cowcode==165 & year==1983
replace no_party=1 if cowcode==350 & year==1974
replace no_party=1 if cowcode==450 & year==1997
replace no_party=1 if cowcode==451 & year==1996
replace no_party=1 if cowcode==452 & year==1979
replace no_party=1 if cowcode==475 & year==1979
replace no_party=1 if cowcode==475 & year==1999
replace no_party=1 if cowcode==640 & year==1983
replace no_party=1 if cowcode==770 & year==1988
replace no_party=1 if cowcode==790 & year==1991

**Libya is incorrectly filled forward (ASU was disbanded in 1977)
replace t=. if cowcode==620 & year>1977


**Marxist regimes
rename Marxist marx
replace marx=0 if marx==. & auth_sample==1 
replace marx=1 if cowcode==110 & year==1992
replace marx=1 if cowcode==101 & year>2000 & year<2005
replace marx=1 if cowcode==290 & year==1989
replace marx=1 if cowcode==310 & year==1990
replace marx=1 if cowcode==339 & year==1992
replace marx=1 if cowcode==355 & year==1990
replace marx=1 if cowcode==360 & year==1990
replace marx=1 if cowcode==365 & year>1971 & year<1992
replace marx=1 if cowcode==404 & year>1993 & year<1998
replace marx=1 if cowcode==439 & year>1971 & year<2016
replace marx=1 if cowcode==541 & year>1993 & year<2004
replace marx=1 if cowcode==712 & year>1989 & year<1992
replace marx=1 if cowcode==402 & year==1991
replace marx=1 if cowcode==433 & year==2000
replace marx=1 if cowcode==438 & year==1984
replace marx=1 if cowcode==520 & year==1991
replace marx=1 if cowcode==700 & year==1992
replace marx=1 if cowcode==775 & year==1988

**Communist Europe
gen ce=0
replace ce=1 if year>1945 & year<1991 & (cowcode==290 | cowcode==310 | cowcode==360 | cowcode==355 | cowcode==315 | cowcode==265)

**Merging Urban and gdpcap
drop _merge
sort cowcode year
merge cowcode year using urban_gdpcap

**Vdem Urban and gdcap for missings
drop _merge
sort cowcode year
merge cowcode year using vdemgrowth_urban

replace e_miurbani=e_miurbani*100
replace urban= e_miurbani if urban==. & e_miurbani~=.

gen logvdemgdp=ln(e_migdppc)
rename logvdemgdp ln_gdpcap
replace ln_gdpcap=loggdpcap if loggdpcap~=. & ln_gdpcap==.

*Fill in Urban missings (used UN figures in spreadsheets in folder (Copy of WUP...))
replace urban=38.9 if cowcode==345 & year==1972
replace urban=42.6 if cowcode==345 & year==1975
replace urban=46.2 if cowcode==345 & year==1980
replace urban=48.4 if cowcode==345 & year==1985
replace urban=50.3 if cowcode==345 & year==1990
sort cowcode year
by cowcode: carryforward urban if cowcode==345, replace
replace urban=22.193 if cowcode==531 & year==2015
replace urban=60.7 if cowcode==731 & year==2015

**fill in lngdpcap
sort cowcode year
by cowcode: carryforward ln_gdpcap if cowcode==531, replace


***Party predates regime
gen predate=1 if Party_Age>t
replace predate=0 if Party_Age~=. & t~=. & Party_Age<=t

*Fixing errors that I found during manual check on December 8, 2021
replace predate=1 if regimeid==262
replace predate=1 if regimeid==96
replace predate=1 if regimeid==40
replace predate=1 if regimeid==55
replace predate=1 if regimeid==252
replace predate=0 if regimeid==19
replace predate=0 if regimeid==209
replace predate=0 if regimeid==208
replace predate=0 if regimeid==15
replace predate=0 if regimeid==214
replace predate=0 if regimeid==326
replace predate=0 if regimeid==189




***Social Org Index
gen org_index=rparty_lorg+rparty_soc+rparty_off
gen org_index_o=rparty_lorg_o+rparty_soc_o+rparty_off_o
**Elite indiex
gen elite_index=rparty_nom_rev+rparty_pers

***Merging Revolutionary regimes
sort cowcode year
drop _merge
merge cowcode year using merge_rev
drop _merge
replace rev=0 if rev==.


***Survival Setup
stset t, failure(fail)

*Section 9
***Coding variables for robustness
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************


***Robustness Regime Type
*Boix, Miller, Rosato
gen b_auth=1 if Party_Regime~=.
replace b_auth=0 if Party_Regime==. 
sort cowcode year
replace b_auth=1 if fail==1 & year>1972 & b_auth!=1 & b_auth[_n-1]==1 & regimeid[_n-1]==regimeid

*GWF coding
gen g_auth=1 if gwf_party~=.
replace g_auth=0 if gwf_party==.

sort cowcode year
replace g_auth=1 if fail==1 & year>1972 & g_auth!=1 & g_auth[_n-1]==1 & regimeid[_n-1]==regimeid

**Dealing with instances where because of December 31 rules in these BMR and Geddes, you have a failure from t-1 that is not autocratic but b_auth or g_auth is coded 1 because the current regime in t started in year t-1 (and is coded as autocratic on Dec 31)
*Use this code to visualize.  Some in this code are not actual problems.  Checked 11/21/20 
*br cowcode country year regimeid auth_sample b_auth g_auth fail regimeid if year>1972 & fail==1 & year>1972 & b_auth==1 & b_auth[_n-1]~=1 & regimeid[_n-1]==regimeid & regimeid[_n+1]~=regimeid & b_auth[_n-1]==0 

*br cowcode country year regimeid auth_sample b_auth g_auth fail regimeid if year>1972 & fail==1 & year>1972 & g_auth==1 & g_auth[_n-1]~=1 & regimeid[_n-1]==regimeid & regimeid[_n+1]~=regimeid & g_auth[_n-1]==0 

replace g_auth=0 if cowcode==40 & year==2000
replace b_auth=0 if cowcode==90 & year==1982
replace b_auth=0 if cowcode==91 & year==2009
replace b_auth=0 if cowcode==135 & year==1990
replace b_auth=0 if cowcode==160 & year==1976
replace b_auth=0 if cowcode==370 & year==1994
replace b_auth=0 if cowcode==420 & year==1981
replace b_auth=0 if cowcode==475 & year==1983
replace b_auth=0 if cowcode==482 & year==2003
replace b_auth=0 if cowcode==500 & year==1985
replace b_auth=0 if cowcode==580 & year==2009
replace b_auth=0 if cowcode==625 & year==1989
replace b_auth=0 if cowcode==770 & year==1977
replace b_auth=0 if cowcode==770 & year==1999





***Creating country year variables for these other autocracy codings and some other cleaning
do g_var.do

do b_var.do


***Data Errors discovered while looking again at Miller discrepancies (dropping by making party_age missing)
replace Party_Age=. if cowcode==145 & year>1979 & year<1983
replace no_party=1 if cowcode==145 & year>1979 & year<1983

replace Party_Age=. if cowcode==482 & year==1986
replace no_party=1 if cowcode==482 & year==1986

replace Party_Age=. if cowcode==800 & year==1976
replace no_party=1 if cowcode==800 & year==1976

replace Party_Age=. if cowcode==950 & year>1986 & year<1992
replace no_party=1 if cowcode==950 & year>1986 & year<1992




***Creating versions of relevant variables for "No party regime" robustness checks
**No party versions of variables
gen share_n=share
replace share_n=0 if no_party==1
gen rparty_lorg_n=rparty_lorg
replace rparty_lorg_n=-2.7 if no_party==1
gen rparty_pers_n=rparty_pers
replace rparty_pers_n=3.8 if no_party==1
gen rparty_off_n=rparty_off
replace rparty_off_n=-2.6 if no_party==1
gen rparty_soc_n=rparty_soc
replace rparty_soc_n=-2.8 if no_party==1
gen leader_nominates_n=leader_nominates
replace leader_nominates_n=1 if no_party==1
gen party_age_n=Party_Age
replace party_age_n=0 if no_party==1
gen log_age_n=ln(party_age_n)
replace log_age_n=0 if no_party==1
gen marx_n=marx
replace marx_n=0 if no_party==1
gen rev_n=rev
replace rev_n=0 if no_party==1
gen predate_n=predate
replace predate=0 if no_party==1
gen org_index_n=org_index
replace org_index_n=-5.9 if no_party==1
gen rparty_nom_n=rparty_nom
replace rparty_nom_n=-.97 if no_party==1
gen elite_index_n=elite_index
replace elite_index_n=-3.088 if no_party==1



****Section 10
***Labels
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************
****************************************************************************************************************************************************


label variable t "Regime Age"
label variable t2 "Regime Age Squared"
label variable t3 "Regime Age Cubed"
label variable log_age "Logged Party Age"
label variable v2x_polyarchy "VDem Polyarchy Score"
label variable marx "Communist Regime"
label variable urban "Urbanization"
label variable ln_gdpcap "Logged GDP/Capita"
label variable rparty_off "Local Org. Extensiveness"
label variable rparty_lorg "Local Org. Activity"
label variable rparty_soc "Ties to Social. Orgs"
label variable org_index "Mass Org. Index"
label variable rparty_pers "Party Personalization"
label variable rparty_nom "Collective Nomination"
label variable rparty_schism "Elite Schisms"
label variable predate "Party Predates Regime"
label variable elite_index "Personalization Index"
label variable share "Vote/Seat Share"
label variable rev "Revolutionary Regime"
label variable ideology_hist "Ideology"
label variable rparty_client "Clientelist Appeals"
label variable leader_nominates "Leader Nominations"
label variable defections "Defections"
label variable fail "Regime Failure"


label variable log_age_n "Logged Party Age"
label variable marx_n "Communist Regime"

label variable rparty_off_n "Local Org. Extensiveness"
label variable rparty_lorg_n "Local Org. Activity"
label variable rparty_soc_n "Ties to Social. Orgs"
label variable org_index_n "Mass Org. Index"
label variable rparty_pers_n "Party Personalization"
label variable rparty_nom_n "Collective Nomination"
label variable predate_n "Party Predates Regime"
label variable elite_index_n "Personalization Index"




label variable marx_g "Communist Regime"

label variable rparty_off_g "Local Org. Extensiveness"
label variable rparty_lorg_g "Local Org. Activity"
label variable rparty_soc_g "Ties to Social. Orgs"
label variable org_index_g "Mass Org. Index"
label variable rparty_pers_g "Party Personalization"
label variable rparty_nom_g "Collective Nomination"
label variable elite_index_g "Personalization Index"


label variable marx_b "Communist Regime"

label variable rparty_off_b "Local Org. Extensiveness"
label variable rparty_lorg_b "Local Org. Activity"
label variable rparty_soc_b "Ties to Social. Orgs"
label variable org_index_b "Mass Org. Index"
label variable rparty_pers_b "Party Personalization"
label variable rparty_nom_b "Collective Nomination"
label variable elite_index_b "Personalization Index"

***

save duration, replace
